<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="发送账户" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入发送账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户类型" prop="accountType">
        <el-select
          v-model="queryParams.accountType"
          placeholder="请选择账户类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.app_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="告警类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择告警类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.app_alarm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="isSlove">
        <el-select
          v-model="queryParams.isSlove"
          placeholder="请选择处理状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.app_slove_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送状态" prop="isSend">
        <el-select
          v-model="queryParams.isSend"
          placeholder="请选择发送状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.app_send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间范围">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-setting"
          size="mini"
          @click="handleSet"
          v-hasPermi="['system:post:export']"
          >设置阈值</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" type="index" align="center" width="50">
      </el-table-column>
      <el-table-column label="告警内容" align="center" prop="sendMsg" />
      <el-table-column label="发送账户" align="center" prop="account" />
      <el-table-column label="账户类型" align="center" prop="accountType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.app_user_type"
            :value="scope.row.accountType"
          />
        </template>
      </el-table-column>
      <el-table-column label="告警类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.app_alarm_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column label="发送状态" align="center" prop="isSend">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.app_send_status"
            :value="scope.row.isSend"
          />
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="isSlove">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.app_slove_status"
            :value="scope.row.isSlove"
          />
        </template>
      </el-table-column>
      <el-table-column label="发送失败原因" align="center" prop="errorMsg" />
      <el-table-column
        label="发送时间"
        align="center"
        prop="sendTime"
        sortable="custom"
        :sort-orders="['descending', 'ascending']"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
            >告警处理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改处理状态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="发送账户" prop="account">
              <el-input
                :disabled="true"
                v-model="form.account"
                placeholder="请输入发送账户"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户类型" prop="accountType">
              <el-select
                :disabled="true"
                v-model="form.accountType"
                placeholder="请选择账户类型"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.app_user_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="String(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="告警类型" prop="type">
              <el-select
                :disabled="true"
                v-model="form.type"
                placeholder="请选择告警类型"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.app_alarm_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="发送状态" prop="isSend">
              <el-select
                :disabled="true"
                v-model="form.isSend"
                placeholder="请选择发送状态"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.app_send_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="发送失败原因" prop="errorMsg">
              <el-input
                :disabled="true"
                v-model="form.errorMsg"
                placeholder="请输入发送失败原因"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="发送时间" prop="sendTime">
              <el-date-picker
                :disabled="true"
                v-model="form.sendTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择发送时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="告警内容" prop="sendMsg">
              <el-input
                :disabled="true"
                v-model="form.sendMsg"
                type="textarea"
                placeholder="请输入告警内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="处理状态">
              <el-radio-group v-model="form.isSlove">
                <el-radio label="1">已处理</el-radio>
                <el-radio label="0">未处理</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 设置阈值对话框 -->
    <el-dialog
      title="告警阈值设置"
      :visible.sync="openSet"
      width="300px"
      append-to-body
    >
      <el-form
        ref="setform"
        :model="setform"
        :rules="setrules"
        label-width="100px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="CPU阈值(%)" prop="cpu">
              <el-select
                v-model="setform.cpu"
                placeholder="请选择CPU阈值"
                clearable
                style="width: 100px"
              >
                <el-option
                  v-for="dict in dict.type.app_cpu_max"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内存阈值(%)" prop="mem">
              <el-select
                v-model="setform.mem"
                placeholder="请选择内存阈值"
                clearable
                style="width: 100px"
              >
                <el-option
                  v-for="dict in dict.type.app_mem_max"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="磁盘阈值(%)" prop="disk">
              <el-select
                v-model="setform.disk"
                placeholder="请选择磁盘阈值"
                clearable
                style="width: 100px"
              >
                <el-option
                  v-for="dict in dict.type.app_disk_max"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="JVM阈值(%)" prop="jvm">
              <el-select
                v-model="setform.jvm"
                placeholder="请选择JVM阈值"
                clearable
                style="width: 100px"
              >
                <el-option
                  v-for="dict in dict.type.app_jvm_max"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSetForm">确 定</el-button>
        <el-button @click="setCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAlarmlist,
  updateAlarm,
  delAlarm,
  getConfig,
  updateSet,
} from "@/api/monitor/alarm";

export default {
  name: "Post",
  dicts: [
    "app_send_status",
    "app_user_type",
    "app_alarm_type",
    "app_slove_status",
    "app_cpu_max",
    "app_disk_max",
    "app_mem_max",
    "app_jvm_max",
  ],
  data() {
    return {
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //阈值弹出
      openSet: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: undefined,
        accountType: undefined,
        type: undefined,
        isSend: undefined,
        isSlove: undefined,
      },
      // 表单参数
      form: {},
      //阈值表单参数
      setform: {},
      // 表单校验
      rules: {},
      // 阈值表单校验
      setrules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getAlarmlist(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    setCancel() {
      this.openSet = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 阈值按钮操作 */
    handleSet() {
      this.openSet = true;
      getConfig().then((response) => {
        this.setform = response.data;
        this.$set(this.setform, "jvm", response.data.jvm);
        this.$set(this.setform, "disk", response.data.disk);
        this.$set(this.setform, "mem", response.data.mem);
        this.$set(this.setform, "cpu", response.data.cpu);
      });
    },
    submitSetForm: function () {
      this.$refs["setform"].validate((valid) => {
        if (valid) {
          updateSet(this.setform).then((response) => {
            this.$modal.msgSuccess("设置成功");
            this.openSet = false;
            this.getList();
          });
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = row.id || this.ids;
      this.title = "告警处理";
      this.open = true;
      this.$set(this.form, "id", row.id);
      this.$set(this.form, "sendMsg", row.sendMsg);
      this.$set(this.form, "accountType", String(row.accountType));
      this.$set(this.form, "type", row.type);
      this.$set(this.form, "errorMsg", row.errorMsg);
      this.$set(this.form, "account", row.account);
      this.$set(this.form, "isSend", String(row.isSend));
      this.$set(this.form, "sendTime", row.sendTime);
      this.$set(this.form, "isSlove", String(row.isSlove));
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateAlarm(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id || this.ids;
      this.$modal
        .confirm("是否删除选中告警记录？")
        .then(function () {
          return delAlarm(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        // "system/post/export",
        "monitor/alarm/record/export",
        {
          ...this.queryParams,
        },
        `自动报警记录_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
